<template>
  <div>
    <category :scene="scene" />
    <el-card style="margin: 10px 0">
      <div v-show="scene === 0">
        <el-button
          type="primary"
          icon="plus"
          :disabled="!categoryStore.category3Id"
          @click="addSpu"
        >
          新增SPU
        </el-button>
        <el-table border style="margin: 10px 0" :data="spuList">
          <el-table-column
            label="序号"
            type="index"
            width="80"
            align="center"
          ></el-table-column>
          <el-table-column
            label="SPU名称"
            prop="spuName"
            width="150"
          ></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="SPU操作">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                icon="plus"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="edit"
                @click="editSpu(row)"
                title="修改spu"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="view"
                title="查看sku列表"
                @click="getSkuList(row)"
              ></el-button>
              <el-popconfirm
                :title="`确定删除${row.spuName}吗？`"
                width="200"
                @confirm="deleteSpu(row.id)"
                confirm-button-text="确定"
                cancel-button-text="取消"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    icon="delete"
                    title="删除"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[3, 6, 9, 12]"
          :size="size"
          :disabled="disabled"
          :background="background"
          layout=" prev, pager, next, jumper, ->,sizes, total"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <el-dialog v-model="skuDialogVisible" title="sku列表">
          <el-table :data="skuList" border>
            <el-table-column label="sku名称" prop="skuName"></el-table-column>
            <el-table-column label="sku描述" prop="skuDesc"></el-table-column>
            <el-table-column label="sku价格" prop="price"></el-table-column>
            <el-table-column label="sku重量" prop="weight"></el-table-column>
            <el-table-column label="sku图片" prop="skuImg">
              <template #default="{ row }">
                <el-image
                  :src="row.skuDefaultImg"
                  style="width: 100px; height: 100px"
                ></el-image>
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
      </div>
      <div v-show="scene === 1">
        <!--        spu添加或修改-->
        <spu-form ref="spuFormRef" @changeScene="changeScene" />
      </div>
      <div v-show="scene === 2">
        <!--        sku添加-->
        <sku-form ref="skuRef" @changeScene="changeScene" />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="index">
import Category from '@/components/Category/index.vue'
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
import { onBeforeUnmount, ref, watch } from 'vue'
import {
  reqSpuPageList,
  reqGetSkuList,
  reqDeleteSpu,
} from '@/api/product/spu/index.ts'
import type {
  SpuList,
  SpuResponseData,
  SpuInfo,
  SkuData,
} from '@/api/product/spu/type.ts'
import useCategoryStore from '@/store/modules/category.ts'
import { ElMessage } from 'element-plus'

let categoryStore = useCategoryStore()
//监听三级分类id
watch(
  () => categoryStore.category3Id,
  (newValue) => {
    spuList.value = []
    if (newValue) {
      getSpuList()
    }
  },
)
const getSpuList = async (page: number = 1) => {
  currentPage.value = page
  const res: SpuResponseData = await reqSpuPageList(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id,
  )
  console.log(res)
  if (res.code === 200) {
    spuList.value = res.data.records
    total.value = res.data.total
  }
}
let spuList = ref<SpuList>([])
//场景切换
const scene = ref(0)
//分页数据
const currentPage = ref(1)
const pageSize = ref(3)
const total = ref(0)
const size = ref('small')
const disabled = ref(false)
const background = ref(true)
let spuFormRef = ref()

//sku列表
let skuList = ref<SkuData[]>([])
//sku列表弹窗
let skuDialogVisible = ref(false)
const handleSizeChange = (val: number) => {
  console.log(`每页 ${val} 条`)
  getSpuList()
}
const handleCurrentChange = (val: number) => {
  console.log(`当前页: ${val}`)
  getSpuList(val)
}

const addSpu = () => {
  scene.value = 1
  spuFormRef.value.initAddData(categoryStore.category3Id)
}
let skuRef = ref()
//添加sku
const addSku = (row: SpuInfo) => {
  scene.value = 2
  skuRef.value.initSkuData(row)
}
//修改spu
const editSpu = (row: SpuInfo) => {
  scene.value = 1
  //调用子组件实例方法获取数据
  spuFormRef.value.initSpuData(row)
}
//场景切换
const changeScene = ({ flag, id }: { flag: number; id: string | number }) => {
  scene.value = flag
  //查询spu数据
  if (id) {
    getSpuList(currentPage.value)
  } else {
    getSpuList()
  }
}

//获取spu的sku数据
const getSkuList = async (row: SpuInfo) => {
  skuDialogVisible.value = true
  let res = await reqGetSkuList(row.id as number)
  if (res.code === 200) {
    console.log(res.data)
    skuList.value = res.data
  }
}
//删除SPU
const deleteSpu = async (spuId: number | string) => {
  console.log('删除SPU', spuId)
  let res = await reqDeleteSpu(spuId)
  if (res.code === 200) {
    ElMessage.success('删除成功')
    getSpuList()
  } else {
    ElMessage.error('删除失败')
  }
}

onBeforeUnmount(() => {
  //组件销毁清空仓库数据
  categoryStore.$reset()
})
</script>

<style scoped lang="scss"></style>
